<template>
  <view class="page-cust-index">
    <van-empty description="暂无数据" v-if="list.length == 0" />
    <view class="list" v-else>
      <van-cell-group  inset v-for="(item,index) in list" :key="index">
        <van-cell use-label-slot>
          <view slot="title">
            <text class="name">{{item.custName}}</text>
            <text class="sex m-x-10">{{item.sex}}</text>
            <text class="age">{{item.age + '岁'}}</text>
          </view>
          <view slot="label">
            <text class="phone">{{item.phone}}</text>
            <van-divider />
            <view class="handle flex-between">
              <view v-if="pageParams.type == 'checkedCust'" @tap.stop="selectCust(item)">
                <van-icon size="17px" name="checked" class="c-green" v-if="item.custId == pageParams.id" />
                <van-icon size="17px" name="circle" v-else />
                <text>选择</text>
              </view>
              <!-- <view v-else @tap.stop="setDefault(item)">
                <van-icon size="17px" name="checked" class="c-green" v-if="item.isDefaulted" />
                <van-icon size="17px" name="circle"  v-else />
                <text>{{item.isDefaulted ? '当前默认' : '设为默认'}}</text>
              </view> -->
              <view class="flex-block">
                <view @tap="$utils.gotoPage('/packageA/cust/addEdit', item)"><van-icon name="edit" /><text>编辑</text></view>
                <view class="m-l-15" @tap="deleteCust(item.custId)"><van-icon name="delete-o" /><text>删除</text></view>
              </view>
            </view>
          </view>
        </van-cell>
      </van-cell-group>
    </view>
    <van-submit-bar class="submit-bar-full" button-type="primary" button-text="添加地址" @submit="$utils.gotoPage('/packageA/cust/addEdit')" />
  </view>
</template>
<script>
  export default{
    data(){
      return{
        pageParams: {},
        list: [
          {custId:1, custName: '张三', phone: '13800138000', sex:1, age: 20 }
        ]
      }
    },
    onLoad(e){
      this.pageParams = e; // 如果有参数，就代表是从个人信息过来选取地址的
    },
    onShow(){
      this.getList();
    },
    methods:{
      getList(){
        this.$http('get', '/nursing/vcs/vip/cust', {}, (data,msg) => {
          this.list = data
        })
      },
      setDefault(item){
        if(item.isDefaulted){
          return;
        }
        let params = Object.assign(item, {isDefault: 1});
        uni.showModal({
          title: '提示',
          content: '确定要设置为默认就诊人吗？',
          success: (res) => {
            if (res.confirm) {
              this.$http('put', '/customer/xcxUser/apiAdderEdit', params,(data,msg) => {
                uni.showToast({title: '设置成功'})
                this.getList('refresh');
              })
            } else if (res.cancel) {
              
            }
          }
        }) 
      },
      deleteCust(id){
        uni.showModal({
          title: '提示',
          content: '确定要删除该服务对象？',
          success: (res) => {
            if (res.confirm) {
              this.$http('post', '/nursing//vcs/vip/cust//unbindCust', {custId: id},(data,msg) => {
                uni.showToast({title: '删除成功'})
                this.getList('refresh');
              })
            } else if (res.cancel) {
              
            }
          }
        }) 
      },
      selectCust(item){
        this.$store.commit('setCheckedCust', item);
        uni.navigateBack();
      },
      
    }
  }
</script>